<?php
/**
 * @category Goatherd
 * @package Goatherd
 *
 * @author Copyright (c) 2012 Maik Penz <maik@phpkuh.de>
 * @version $Id: ObserverTrait.php 84 2012-03-27 22:24:33Z maik@phpkuh.de $
 *
 * This file is part of Goatherd library.
 *
 * Goatherd library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Goatherd library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Goatherd library. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Goatherd\Commons\Observer;

/**
 * Basic observer implementation.
 * Different subject states can be listened to by event name.
 *
 * For example does method 'handleChanged()' listen to event 'changed'
 * with the method name consiting of 'handle' + uc_first('event_name').
 *
 * @category Goatherd
 * @package Goatherd
 * @subpackage Commons
 */
trait ObserverTrait
{
    /**
     * Active subject
     *
     * @var ISubject
     */
    protected $_observerSubject = null;

    /**
     * Event data as given by subject on notify().
     *
     * @var mixed
     */
    protected $_observerEventData = null;

    /**
     *
     * @return \Goatherd\Commons\Observer\ISubject
     */
    public function getActiveSubject()
    {
        return $this->_observerSubject;
    }

    /**
     *
     * @return mixed
     */
    public function getEventData()
    {
        return $this->_observerEventData;
    }

    /**
     * Inform the observer of changes to the subject object.
     *
     * This method will also retrieve the event (i.e. status) of the caller. If
     * there is a class method 'handleEventname' for event 'eventname' this
     * method is called with the subject being stored in the $this->_subject
     * property.
     *
     * @param SplSubject $subject
     * @see SplObserver#update()
     */
    public function update(\SplSubject $subject)
    {
        if (!($subject instanceof ISubject)) {
            throw new \InvalidArgumentException('Subject must implement Goatherd\Commons\Observer\ISubject.');
        }

        // get event (data)
        $this->_observerEventData = $subject->getEventData();
        $eventName = $subject->getEventName();

        // assume event name to be the first value
        is_array($eventName) and $eventName = current($eventName);

        // no name given for event
        if (!is_string($eventName)) {
            throw new \InvalidArgumentException('Event name must be of type string.');
        }

        // decide which method to call
        $method = 'handle'.ucfirst($eventName);
        $result = null;

        if (method_exists($this, $method)) {
            $this->_observerSubject = $subject;
            $result = $this->$method();
        }

        return $result;
    }
}